package cn.jetpiece.cloud.log.app.modular.log.wrapper;


import cn.jetpiece.cloud.core.util.ResultWrapper;
import cn.jetpiece.cloud.log.api.model.result.GatewayLogResult;
import cn.jetpiece.cloud.model.util.SpringContextHolder;
import cn.jetpiece.cloud.resource.api.ResourceApi;
import cn.jetpiece.cloud.resource.api.mode.result.ResourceResult;

/**
 * <p>
 * 网关日志结果封装器
 * </p>
 *
 * @author XuJZ
 */

public class GatewayLogWrapper extends ResultWrapper<GatewayLogResult> {

    private static ResourceApi resourceApi;

    public GatewayLogWrapper(Object object) {
        super(object);
    }

    @Override
    protected void wrapTheDto(GatewayLogResult dto) {
        resourceApi = getResourceService();
        ResourceResult resourceResult = resourceApi.getByUrl(dto.getRequestPath(), dto.getRequestMethod());
        if (resourceResult != null){
            dto.setLogContent(resourceResult.getName());
            dto.setMethodName(resourceResult.getClassName()+"-"+resourceResult.getMethodName());
        }
    }

    private ResourceApi getResourceService() {
        if (resourceApi == null) {
            resourceApi = SpringContextHolder.getBean(ResourceApi.class);
        }
        return resourceApi;
    }
}
